#!/usr/bin/python
# coding=utf-8
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup

browser = webdriver.Chrome()
wait = WebDriverWait(browser,30)

def search():
    browser.get('https://www.taobao.com/')
    imput = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#q"))
    )
    submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#J_TSearchForm > div.search-button > button")))
    imput.send_keys('美食')
    submit.click()
    total = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.total")))
    get_products()
    return total.text

def next_page(page_number):
    imput = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.form > input"))
    )
    submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit")))
    imput.clear()
    imput.send_keys(page_number)
    submit.click()
    wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'),str(page_number)))
    get_products()


def get_products():
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#mainsrp-itemlist")))
    html = browser.page_source
    soup = BeautifulSoup(html,'lxml')
    for item in soup.find_all('div', class_='ctx-box J_MouseEneterLeave J_IconMoreNew'):
        location = item.find('div', class_='location').text.strip()
        shop = item.find('div', class_='shop').text.strip()
        price = item.find('div', class_='price g_price g_price-highlight').text.strip()
        name = item.find('div', class_='row row-2 title').text.strip()
        deal = item.find('div', class_='deal-cnt').text.strip()
        product = {'name': name,'location': location, 'deal': deal, 'price': price.replace('¥', ''), 'shop': shop}
        print(product)


def main():
        text = search()
        text =int(re.search('(\d+)',text).group(1))
        for i in range(2,text):
            next_page(i)


if __name__ == '__main__':
    main()


